package hw.txtreader.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hw.txtreader.R;
import hw.txtreader.base.BaseRecyclerViewHolder;
import hw.txtreader.databinding.ItemBookBinding;
import hw.txtreader.listener.OnItemClickListener;

public class BookAdapter extends RecyclerView.Adapter<BaseRecyclerViewHolder> {
    private List<String> strings = new ArrayList<>();
    private Context mcontext;
    public OnItemClickListener mOnItemClickLitener;


    public BookAdapter(Context context, List<String> strings) {
        this.mcontext = context;
        this.strings = strings;

    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemBookBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mcontext),
                R.layout.item_book, parent, false);
        binding.setAdapter(this);
        return new TourViewHolder(binding);
    }


    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.bindData(strings.get(position));

        TourViewHolder tv = (TourViewHolder) holder;
        tv.tvModuleName.setText(getName(strings.get(position)));

        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public class TourViewHolder extends BaseRecyclerViewHolder<String> {
        ItemBookBinding binding;
        TextView tvModuleName;

        public TourViewHolder(ItemBookBinding dataBinding) {
            super(dataBinding.getRoot());
            binding = dataBinding;
            tvModuleName = dataBinding.tvModuleName;
        }

        @Override
        public void bindData(String data) {
            binding.setBean(data);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return strings.size();
    }

    public String getName(String path) {
        int start = path.lastIndexOf("/");
        int end = path.lastIndexOf(".");
        if (start != -1 && end != -1) {
            return path.substring(start + 1, end);//包含头不包含尾 , 故:头 + 1
        } else {
            return "神秘书籍";
        }
    }
}
